<?php
namespace app\admin\controller;

use think\Request;
use app\admin\model\ServerCenter;

class Service extends Base
{
    protected $model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new ServerCenter();
    }

    public function index()
    {
        $data = $this->model->select();

        $this->assign('data', $data);
        return $this->fetch();
    }

    public function add()
    {
        if (Request::instance()->isPOST()) {
            // var_dump(input("post."));
            // exit;
            $title = trim(input('post.title'));
            
            if (!$title) {
                $this->error('标题不可为空');
            }

            $content = (null!==input('post.editorValue')) ? input('post.editorValue'): '';
            $sort = intval(input('post.sort'));
            $img_title = input('post.thumb');
            $time = time();
            try {
                $this->model->save([
                    'title' => $title,
                    'content' => $content,
                    'sort' => $sort,
                    'img_title' => $img_title,
                    'mtime' => $time,
                    ]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                
                $this->error("错误代码:" . $msg);
            }
            $this->success(config('msg.1003'), 'index');
        }
        
        return $this->fetch();
    }

    public function edit($id)
    {
        $data = $this->model->find($id);
        if (Request::instance()->isPOST()) {
            // var_dump(input("post."));
            // exit;
            $title = trim(input('post.title'));
            
            if (!$title) {
                $this->error('标题不可为空');
            }

            $img_title = input('post.thumb');
            $content = (null!==input('post.editorValue')) ? input('post.editorValue'): '';
            $sort = intval(input('post.sort'));
            $clicks = intval(input('post.clicks'));
            $time = time();
            try {
                $data->save([
                    'title' => $title,
                    'content' => $content,
                    'img_title' => $img_title,
                    'sort'    => $sort,
                    'clicks'  => $clicks,
                    // 'status'  => $status,
                    'mtime' => $time,
                    ]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                
                $this->error("错误代码:" . $msg);
            }
            $this->success(config('msg.1003'), 'index');
        }
        
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function del($id = null)
    {
        if (Request::instance()->isAjax()) {
            if ($this->model->destroy($id)) {
                return ['status'=>1,'msg'=>'删除成功'];
            } else {
                return ['status'=>0,'msg'=>'删除失败'];
            }
        }
    }
}
